﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TCPTest
{
    public class Parser
    {
        string input;

        public Parser()
        {

        }

        private string killWhiteSpace(string str)
        {
            return str.Trim();
        }

        private byte moveParse(char m)
        {
            switch (m)
            {
                case '1': return 0;
                case '2': return 1;
                case '3': return 2;
                case '4': return 3;
                case '5': return 4;
                case '6': return 5;
                case '7': return 6;
                case '8': return 7;
                case '9': return 8;
                case 'a': return 9;
                case 'b': return 10;
                case 'c': return 11;
                case 'd': return 12;
                case 'e': return 13;
                case 'f': return 14;
            }
            throw new Exception("Invalid move input");
        }

        private bool isMove(string str)
        {
            if (str.Length != 2)
                return false;

            return ((str[0] >= '1' && str[0] <= '9') || (str[0] >= 'a' || str[0] <= 'f'))
                && ((str[1] >= '1' && str[1] <= '9') || (str[1] >= 'a' || str[1] <= 'f'));
        }

        public Move parseMove(string str)
        {
            if (!isMove(str))
                throw new Exception("Invalid Move");
            Move move = new Move();
            char m1 = str[0];
            char m2 = str[1];

            move.x = moveParse(m1);
            move.y = moveParse(m2);
            return move;
        }

        public bool parse(string newInput)
        {
            input = killWhiteSpace(newInput);
            input = input.ToLower();

            Console.WriteLine("Parser got this: " + input);

            if (input == "display")
                Console.WriteLine("Toggle display here");
            else if (input == "exit")
                Console.WriteLine("Exit here");
            else if (input == "easy")
                Console.WriteLine("Easy here");
            else if (input == "medium")
                Console.WriteLine("Medium here");
            else if (input == "hard")
                Console.WriteLine("Hard here");
            else if (input == "undo")
                Console.WriteLine("Undo here");
            else if (input == "human-ai")
                Console.WriteLine("Human-AI here");
            else if (input.StartsWith("ai-ai"))
                Console.WriteLine("AI-AI here");
            else if (isMove(input))
            {
                Move move = parseMove(input);
                Game.executeMove(move);
            }
            else
                Console.WriteLine("Comment here");

            return true;
        }

    }
}